<template>
  <div>
    <!--
  websocket
  1：连接
  const ws=   new WebSocket('地址')
  ws.addEventListener('open',()=>{})
  2:接收消息
    ws.addEventListener('message',(e)=>{
      e.data
    })
  3:发送消息
    ws.send('消息内容')
  4:关闭
    ws.close()
    ws.addEventListener('close',()=>{})

 -->
    <van-button @click="btnClick1">连接</van-button>
    <van-field v-model="msg"></van-field>
    <van-button @click="send">发送</van-button>
    <van-button @click="ws.close()">关闭</van-button>
  </div>
</template>
<script>
export default {
  data () {
    return {
      ws: '',
      msg: ''
    }
  },
  methods: {
    btnClick1 () {
      this.ws = new WebSocket('ws://119.91.150.211:1234')
      this.ws.addEventListener('open', () => {
        console.log('连接成功')
      })
      this.ws.addEventListener('message', e => {
        console.log(e.data)
      })
      this.ws.addEventListener('close', () => {
        console.log('关闭了')
      })
    },
    send () {
      this.ws.send(this.msg)
    }
  }
}
</script>
<style></style>
>
